package com.code.commons.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证工具类
 *
 * @author tangyifei
 */
public class ValidatorUtil {

    private static final String EMAIL_FORMAT = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    //private static final String MOBILE_FORMAT = "^((\\+86)|(86))?[1][3456789][0-9]{9}$";
    private static final String MOBILE_FORMAT = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";

    private static final Pattern MOBILE_PATTERN = Pattern.compile(MOBILE_FORMAT);

    private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_FORMAT);

    /**
     * 判断是否为手机号
     *
     * @param phone 手机号
     * @return 手机号验证与否
     */
    public static boolean isMobile(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        return true;
//        if (phone.length() != 11) {
//            return false;
//        }
//        Matcher m;
//        //日文
//        if (phone.startsWith("080") || phone.startsWith("090")) {
//            m = Pattern.compile("^(\\+?81|0)\\d{1,4}[ \\-]?\\d{1,4}[ \\-]?\\d{4}$").matcher(phone);
//        } else {
//            m = MOBILE_PATTERN.matcher(phone);
//        }
//        return m.matches();
    }

    /**
     * 判断是否为邮箱
     *
     * @param email 邮箱
     * @return 邮箱验证与否
     */
    public static boolean isEmail(String email) {
        if (StringUtils.isEmpty(email)) {
            return false;
        }
        Matcher m = EMAIL_PATTERN.matcher(email);
        return m.matches();
    }

//	public static void main(String[] args) {
//			System.out.println(isMobile("08085897889"));
////			System.out.println(isMobile("1891234123"));
//	}
}
